package com.example.opengl;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import android.opengl.GLES20;
import android.opengl.GLSurfaceView;
import android.opengl.Matrix;
import android.os.SystemClock;

public class MyGLRenderer implements GLSurfaceView.Renderer {

	Triangle mTriangle;
	Square mSquare;
	
	public volatile float mAngle;
	
    // mMVPMatrix is an abbreviation for "Model View Projection Matrix"
    private final float[] mMVPMatrix = new float[16];
    private final float[] mProjectionMatrix = new float[16];
    private final float[] mViewMatrix = new float[16];

    //la creo para rotar
    private float[] mRotationMatrix = new float[16];
    
	@Override
	public void onDrawFrame(GL10 gl) {
		float[] scratch = new float[16];
		      
		 // Draw background color
		GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
      
        // Set the camera position (View matrix)
        Matrix.setLookAtM(mViewMatrix, 0, 0, 0, -3, 0f, 0f, 0f, 0f, 1.0f, 0.0f);

        // Calculate the projection and view transformation
        Matrix.multiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mViewMatrix, 0);
        
        mSquare.draw(mMVPMatrix);
        
        // Create a rotation transformation for the triangle
        //long time = SystemClock.uptimeMillis() % 4000L;
        //float angle = 0.50f * ((int) time);
        //comente las dos lineas de arriba para activar la rotacion con input, y puse mAngle, en vez de angle
        Matrix.setRotateM(mRotationMatrix, 0, mAngle, 0, 0, -1.0f);

        // Combine the rotation matrix with the projection and camera view
        // Note that the mMVPMatrix factor *must be first* in order
        // for the matrix multiplication product to be correct.
        Matrix.multiplyMM(scratch, 0, mMVPMatrix, 0, mRotationMatrix, 0);

        // Draw triangle
        mTriangle.draw(scratch);
        

	}

	@Override
	//aca calculo la data para una transformacion de proyeccion
	public void onSurfaceChanged(GL10 gl, int width, int height) {
		 GLES20.glViewport(0, 0, width, height);
		 
		 float ratio = (float) width / height;
		 
	    // this projection matrix is applied to object coordinates in the onDrawFrame() method
		Matrix.frustumM(mProjectionMatrix, 0, -ratio, ratio, -1, 1, 3, 7);
	}
	
	@Override
	public void onSurfaceCreated(GL10 gl, EGLConfig config) {
		// Set the background frame color
		//rgba
        GLES20.glClearColor(0.5f, 0.5f, 0f, 1.0f);
        
        mTriangle = new Triangle();
        
        mSquare = new Square();
	}
	
	public static int loadShader(int type, String shaderCode){

	    // create a vertex shader type (GLES20.GL_VERTEX_SHADER)
	    // or a fragment shader type (GLES20.GL_FRAGMENT_SHADER)
	    int shader = GLES20.glCreateShader(type);

	    // add the source code to the shader and compile it
	    GLES20.glShaderSource(shader, shaderCode);
	    GLES20.glCompileShader(shader);

	    return shader;
	}
	
	public float getAngle() {
        return mAngle;
    }
  
  	public void setAngle(float angle) {
        mAngle = angle;
    }

}